package com.song.shop;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan(basePackages = "com.song.shop.dao")
public class ShopApplication {

	final static String queueName = "hello";

    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }
    
    @Bean
    public Queue userQueue() {
    	return new Queue("user");
    }
    
    //===============以下是验证topic Exchange的队列==========
    @Bean
    public Queue queueMessage() {
        return new Queue("topic.message");
    }

    @Bean
    public Queue queueMessages() {
        return new Queue("topic.messages");
    }
  //===============以上是验证topic Exchange的队列==========
    
    
    //===============以下是验证Fanout Exchange的队列==========
    @Bean(name = "AMessage")
    public Queue AMessage() {
        return new Queue("fanout.A");
    }

    @Bean(name="bMessage")
    public Queue BMessage() {
        return new Queue("fanout.B");
    }

    @Bean(name ="cMessage")
    public Queue CMessage() {
        return new Queue("fanout.C");
    }
    
    @Bean(name = "dMessage")
    public Queue DMessage() {
        return new Queue("fanout.D");
    }
    @Bean
    public Queue EMessage() {
        return new Queue("fanout.E");
    }
    //===============以上是验证Fanout Exchange的队列==========
    

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("exchange");
    }
    
    //Fanout模式交换机
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    /**
     * 将队列topic.message与exchange绑定，binding_key为topic.message,就是完全匹配
     * @param queueMessage
     * @param exchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
    }

    /**
     * 将队列topic.messages与exchange绑定，binding_key为topic.#,模糊匹配
     * @param queueMessages
     * @param exchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
    }
    
    /**
     *  将fanout.A fanout.B fanout.C 与 fanoutExchange 关联到一块
     * @param EMessage
     * @param fanoutExchange
     * @return
     */
    @Bean
    Binding bindingExchangeE(Queue EMessage,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(EMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeA(@Qualifier("AMessage") Queue AMessage,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(AMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeB(@Qualifier("bMessage") Queue BMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(BMessage).to(fanoutExchange);
    }
    
    @Bean
    Binding bindingExchangeC(@Qualifier("cMessage")Queue CMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(CMessage).to(fanoutExchange);
    }
    
    @Bean
    Binding bindingExchangeD(@Qualifier("dMessage") Queue DMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(DMessage).to(fanoutExchange);
    }
    
	public static void main(String[] args) {
		SpringApplication.run(ShopApplication.class, args);
	}

	public void test1(){
        System.out.println("测冲突");
    }

    public void test(){
        System.out.println("------测试冲突");
    }

    public void test3(){

    }
}
